home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / libraries / newiff.lha / NewIFF / NewIFF39.lha / newiff39 / apps / ILBMtoRaw / ILBMtoRaw.c < prev    next >
C/C++ Source or Header  |  1993-09-28  |  5KB  |  182 lines

  1. /*--------------------------------------------------------------*/
  2. /*                                */
  3. /* ILBMtoRaw: reads in ILBM, writes out raw file (raw planes,     */
  4. /*  followed by colormap)                     */
  5. /*                                                              */
  6. /* Based on ILBMRaw.c by Jerry Morrison and Steve Shaw,        */
  7. /* Electronic Arts.                               */
  8. /* Jan 31, 1986                            */
  9. /*                                                              */
  10. /* This software is in the public domain.                       */
  11. /* This version for the Commodore-Amiga computer.               */
  12. /*                                                              */
  13. /*  Callable from CLI ONLY                    */
  14. /*  modified 05-91 for use wuth iffparse modules        */
  15. /*  Requires linkage with several other modules - see Makefile  */
  16. /*--------------------------------------------------------------*/
  17. #define INTUI_V36_NAMES_ONLY
  18.  
  19. #include "iffp/ilbmapp.h"
  20.  
  21.  
  22. #ifdef __SASC
  23. void __chkabort(void) {}          /* Disable SAS CTRL-C checking. */
  24. #else
  25. #ifdef LATTICE
  26. void chkabort(void) {}            /* Disable LATTICE CTRL-C checking */
  27. #endif
  28. #endif
  29.  
  30.  
  31. #include "ilbmtoraw_rev.h"
  32. UBYTE vers[] = VERSTAG;
  33. UBYTE Copyright[] = VERS " - Converts ILBM to raw file - Freely Redistributable";
  34.  
  35. void bye(UBYTE *s, int e);
  36. void cleanup(void);
  37.  
  38. LONG SaveBitMap(UBYTE *name, struct BitMap *bm, SHORT *cols, int ncols);
  39.  
  40. struct Library *IFFParseBase = NULL;
  41. struct Library *GfxBase = NULL;
  42.  
  43. /* ILBM frame */
  44. struct ILBMInfo ilbm = {0};
  45.  
  46.  
  47. /* ILBM Property chunks to be grabbed - BMHD and CMAP needed for this app
  48.  */
  49. LONG    ilbmprops[] = {
  50.         ID_ILBM, ID_BMHD,
  51.         ID_ILBM, ID_CMAP,
  52.         TAG_DONE
  53.         };
  54.  
  55. /* ILBM Collection chunks (more than one in file) to be gathered */
  56. LONG    *ilbmcollects = NULL;    /* none needed for this app */
  57.  
  58. /* ILBM Chunk to stop on */
  59. LONG    ilbmstops[] = {
  60.         ID_ILBM, ID_BODY,
  61.         TAG_DONE
  62.         };
  63.  
  64.  
  65. /** main() ******************************************************************/
  66.  
  67. void main(int argc, char **argv)
  68.     {
  69.     LONG error=NULL;
  70.     UBYTE *ilbmname, fname[80], buf[24];
  71.  
  72.     if ((argc < 2)||(argv[argc-1][0]=='?'))
  73.     bye("Usage from CLI: 'ILBMtoRaw filename'\n",RETURN_OK);
  74.     
  75.     if(!(GfxBase = OpenLibrary("graphics.library",0)))
  76.     bye("Can't open graphics.library",RETURN_FAIL);
  77.  
  78.     if(!(IFFParseBase = OpenLibrary("iffparse.library",0)))
  79.     bye("Can't open iffparse.library",RETURN_FAIL);
  80.  
  81. /*
  82.  * Here we set up default ILBMInfo fields for our
  83.  * application's frames.
  84.  * Above we have defined the propery and collection chunks
  85.  * we are interested in (some required like BMHD)
  86.  */
  87.     ilbm.ParseInfo.propchks      = ilbmprops;
  88.     ilbm.ParseInfo.collectchks   = ilbmcollects;
  89.     ilbm.ParseInfo.stopchks      = ilbmstops;
  90.     if(!(ilbm.ParseInfo.iff = AllocIFF()))
  91.         bye(IFFerr(IFFERR_NOMEM),RETURN_FAIL);    /* Alloc an IFFHandle */
  92.  
  93.     ilbmname = argv[1];
  94.  
  95.     /* Load as a brush since we don't need to display it */
  96.     if (error = loadbrush(&ilbm,ilbmname))
  97.         {
  98.         printf("Can't load ilbm \"%s\", ifferr=%s\n",ilbmname,IFFerr(error));
  99.         bye("",RETURN_WARN);
  100.         }
  101.     else /* Successfully loaded ILBM */
  102.     {
  103.         strcpy(fname,argv[1]);
  104.  
  105.     if(ilbm.camg & HAM)    strcat(fname, ".ham");
  106.     if(ilbm.camg & EXTRA_HALFBRITE)    strcat(fname, ".ehb");
  107.  
  108.     if(ilbm.camg & HIRES)    strcat(fname, ".hi");
  109.     else strcat(fname, ".lo");
  110.  
  111.     if(ilbm.camg & LACE)    strcat(fname, ".lace");
  112.  
  113.     strcat(fname,".");
  114.     sprintf(buf,"%d",ilbm.Bmhd.w);
  115.     strcat(fname,buf);
  116.     strcat(fname,"x");
  117.     sprintf(buf,"%d",ilbm.Bmhd.h);
  118.     strcat(fname,buf);
  119.     strcat(fname,"x");
  120.     sprintf(buf,"%d",ilbm.brbitmap->Depth);
  121.     strcat(fname, buf);
  122.     printf(" Creating file %s \n", fname);
  123.     error=SaveBitMap(fname, ilbm.brbitmap, ilbm.colortable, ilbm.ncolors);
  124.  
  125.     unloadbrush(&ilbm);
  126.     }
  127.  
  128.     if(error)    bye(IFFerr(error),RETURN_WARN);
  129.     else    bye("",RETURN_OK);
  130.     }
  131.  
  132.  
  133. /* SaveBitMap (as raw planes and colortable)
  134.  *
  135.  * Given filename, bitmap structure, and colortable pointer,
  136.  * writes out raw bitplanes and colortable (not an ILBM)
  137.  * Returns 0 for success
  138.  */
  139.  
  140.  
  141. LONG SaveBitMap(UBYTE *name, struct BitMap *bm, SHORT *cols, int ncols)
  142.     {
  143.     SHORT i;
  144.     LONG nb,plsize;
  145.  
  146.     LONG file = Open( name, MODE_NEWFILE);
  147.     if( file == 0 )
  148.     {
  149.     printf(" couldn't open %s \n",name);
  150.     return(CLIENT_ERROR);    /* couldnt open a load-file */    
  151.     }
  152.     plsize = bm->BytesPerRow*bm->Rows;
  153.     for (i=0; i<bm->Depth; i++)
  154.     {
  155.     nb =  Write(file, bm->Planes[i], plsize);
  156.     if (nb<plsize) break;
  157.     }
  158.     if(nb>0)    nb=Write(file, cols, (1<<bm->Depth)*2);    /* save color map */
  159.     Close(file);
  160.     return(nb >= 0 ? 0L : IFFERR_WRITE);
  161.     }
  162.  
  163. void bye(UBYTE *s, int e)
  164.     {
  165.     if(s&&(*s))    printf("%s\n",s);
  166.     cleanup();
  167.     exit(e);
  168.     }
  169.  
  170. void cleanup()
  171.     {
  172.     if(ilbm.ParseInfo.iff)        FreeIFF(ilbm.ParseInfo.iff);
  173.  
  174.     if(IFFParseBase)    CloseLibrary(IFFParseBase);
  175.     if(GfxBase)        CloseLibrary(GfxBase);
  176.     }
  177.  
  178.  
  179.  
  180.  
  181.  
  182.